package cn.wolfcode.wolf2w.service.impl;

import cn.wolfcode.wolf2w.domain.StrategyComment;
import cn.wolfcode.wolf2w.exception.LogicalException;
import cn.wolfcode.wolf2w.query.StrategyCommentQueryObject;
import cn.wolfcode.wolf2w.repository.StrategyCommentRepository;
import cn.wolfcode.wolf2w.service.IStrategyCommentService;
import cn.wolfcode.wolf2w.util.PagingUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

@Service
//@Transactional
public class StrategyCommentServiceImpl implements IStrategyCommentService {

    @Autowired
    private StrategyCommentRepository repository;

    @Autowired
    private MongoTemplate template;


    @Override
    public void save(StrategyComment strategyComment) {
        strategyComment.setId(null);   //设为空值,防止数据库不会自动创建id

        repository.save(strategyComment);
    }

    @Override
    public void delete(String id) {
        repository.deleteById(id);

    }

    @Override
    public void update(StrategyComment strategyComment) {
        repository.save(strategyComment);
    }

    @Override
    public StrategyComment get(String id) {
        return repository.findById(id).orElse(null);
    }

    @Override
    public List<StrategyComment> list() {
        return repository.findAll();
    }

    @Override
    public Page<StrategyComment> query(StrategyCommentQueryObject qo) {

        //查询封装类
        Query query = new Query();
        //判断参数攻略id
        if (StringUtils.hasLength(qo.getStrategyId())){
            //条件
            query.addCriteria(Criteria.where("strategyId").is(qo.getStrategyId()));
        }

        //查询分页
         Pageable pageable = PageRequest.of(qo.getCurrentPage()-1, qo.getPageSize(), Sort.Direction.DESC, "createTime");

        //调用工具方法,返回分页数据
        return PagingUtil.query(query, template, StrategyComment.class, pageable);

    }

    @Override
    public void saveOrUpdate(StrategyComment strategyComment) {
        //判断是否有id
        if (StringUtils.hasLength(strategyComment.getId())){
            //这里是全量更新
            this.update(strategyComment);
        }else {
            this.save(strategyComment);
        }
    }

    @Override
    public void commentThumb(String cid, String uid) {
        //校验参数
        if (!StringUtils.hasLength(cid)){
            throw new LogicalException("参数异常");
        }
        //校验参数
        if (!StringUtils.hasLength(uid)){
            throw new LogicalException("请先登录");
        }
        //获取点赞集合
        StrategyComment strategyComment = this.get(cid);
        List<String> thumbuplist = strategyComment.getThumbuplist();
        //判断用户id 是否在集合中
        if (thumbuplist.contains(uid)){
            //存在
            //点赞数-1,将用户id移除集合
            strategyComment.setThumbupnum(strategyComment.getThumbupnum() - 1 );
            thumbuplist.remove(uid);
        }else {
           //不存在
           //点赞数+1,将用户id存到集合中
            strategyComment.setThumbupnum(strategyComment.getThumbupnum() + 1 );
            thumbuplist.add(uid);
        }

        //更新数据
        this.update(strategyComment);

    }


}
